package com.mvd.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.mvd.app.R;
import com.mvd.app.adapter.OtherWaiJianPhotoAdapter;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.Constants;
import com.mvd.app.model.OtherWaiJianPhotoModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 其他外检照片
 **/
public class OtherWaiJianPhotoActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.title_btn_left)
    ImageButton titleBtnLeft;
    @BindView(R.id.title_btn_left_txt)
    Button titleBtnLeftTxt;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.iv_headtip)
    ImageView ivHeadtip;
    @BindView(R.id.title_btn_right_txt)
    Button titleBtnRightTxt;
    @BindView(R.id.title_btn_right)
    ImageButton titleBtnRight;
    @BindView(R.id.ac_wj_photo_lv_main)
    ListView acWjPhotoLvMainListView;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.ac_waijian_btn_ok)
    Button acWaijianBtnOk;
    @BindView(R.id.ac_waijian_photo_ll_bottom_layout)
    LinearLayout acWaijianPhotoLlBottomLayout;
    @BindView(R.id.ac_waijian_photo_scrollview)
    ScrollView acWaijianPhotoScrollview;

    private OtherWaiJianPhotoActivity instances = null;
    private OtherWaiJianPhotoAdapter OtherWaiJianPhotoAdapter;
    private List<OtherWaiJianPhotoModel> buHeGeModel3List = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wai_jian_other_photo);
        ButterKnife.bind(this);
        instances = this;

        buHeGeModel3List = new ArrayList<OtherWaiJianPhotoModel>();

        acWjPhotoLvMainListView.setOnItemClickListener(this);

        OtherWaiJianPhotoModel OtherWaiJianPhotoModel = null;
        for (int i = 0; i < Constants.WAIJIAN_PHOTO_LISTS.length; i++) {
            OtherWaiJianPhotoModel = new OtherWaiJianPhotoModel();
            OtherWaiJianPhotoModel.setCode(Constants.WAIJIAN_PHOTO_LISTS[i][1]);
            OtherWaiJianPhotoModel.setName(Constants.WAIJIAN_PHOTO_LISTS[i][0]);
            buHeGeModel3List.add(OtherWaiJianPhotoModel);
        }
        OtherWaiJianPhotoAdapter = new OtherWaiJianPhotoAdapter(instances, buHeGeModel3List, null);
        acWjPhotoLvMainListView.setAdapter(OtherWaiJianPhotoAdapter);
        setListViewHeightBasedONChildren(acWjPhotoLvMainListView);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        // 取得ViewHolder对象
        OtherWaiJianPhotoAdapter.ViewHolder viewHolder = (OtherWaiJianPhotoAdapter.ViewHolder) view.getTag();
        if (viewHolder.mCheckBox.isChecked()) {
            viewHolder.mCheckBox.setChecked(false);
            Log.e("tag", position + "==取消了选择");
            buHeGeModel3List.get(position).setIsCheck("0");
        } else {
            viewHolder.mCheckBox.setChecked(true);
            buHeGeModel3List.get(position).setIsCheck("1");
            Log.e("tag", buHeGeModel3List.get(position).toString());
        }
        boolean check = viewHolder.mCheckBox.isChecked();
        OtherWaiJianPhotoAdapter.map.put(position, check);
//        for (int i = 0; i < buHeGeModel3List.size(); i++) {
//            if ("1".equals(buHeGeModel3List.get(i).getIsCheck())) {
//                BaseApplication.BaseApplication_OtherWaiJianPhotoModelList.add(buHeGeModel3List.get(i));
//
//            } else {
//
//            }
//
//        }
        if (OtherWaiJianPhotoAdapter.map.containsValue(false)) {
            //有选择的操作，即不是全部选中状态
            BaseApplication.IsAllSelect = false;
        }
        if (!OtherWaiJianPhotoAdapter.map.containsValue(false)) {
            BaseApplication.IsAllSelect = true;
        }
    }

    @OnClick({R.id.ac_waijian_btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_waijian_btn_ok:
                List<OtherWaiJianPhotoModel> list =new ArrayList<>();
                for (int i = 0; i < buHeGeModel3List.size(); i++) {
                    if ("1".equals(buHeGeModel3List.get(i).getIsCheck())) {
                        list.add(buHeGeModel3List.get(i));
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("othet_waijian_photo", (Serializable) list);
                this.setResult(404, intent);
                this.finish();
                break;

        }
    }

    public static void setListViewHeightBasedONChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);

    }
}
